/**********************************************************************************
// D3DGame Header File
//
// Creation:	06 Apr 2011
// Updated:		27 Mai 2011
// Compiler:	Visual Studio 2010
//
// Notes:		A D3DGame is a WindowsGame that makes use of Direct3D
//				functions to access the video card aceleration hardware.
//
**********************************************************************************/

#ifndef _ANABELE_DIRECT3DGAME_H_
#define _ANABELE_DIRECT3DGAME_H_

/**********************************************************************************/
// make classes and functions exportable to a DLL
#ifdef ANABELE_EXPORTS
#define DLL __declspec( dllexport ) 
#else
#define DLL __declspec( dllimport )
#endif

/**********************************************************************************/
// Defines
#define INITGUID			// make sure directX guids are included

// Includes
#include "WindowsGame.h"	// draw the game on a window
#include "Types.h"			// engine custom types
#include "Font.h"			// font for engine messages
#include <d3d9.h>			// Direct 3D stuff
#include <d3dx9.h>			// Direct 3D extensions

/**********************************************************************************/

// Global Constants and Enumerations
enum Fps {FIXED30FPS, FIXED60FPS, VARIABLEFPS};

/**********************************************************************************/

class DLL D3DGame : public WindowsGame
{
public:
	static Viewport           viewport;						// portion of gameworld to show onscreen

private:
	// static member variables
	static LPDIRECT3DDEVICE9  d3dDev;						// direct3D device 
	static LPDIRECT3DSURFACE9 backBuffer;					// direct3D back buffer
	static LPDIRECT3DTEXTURE9 drawingTex;					// drawing texture (used for pixel plotting)
	static LPD3DXSPRITE		  renderer;						// sprite renderer
	static int	  			  videoMemoryPitch;				// bytes in each line of Video Memory
	static uint *			  videoMemory;					// pointer to Video Memory (32 bytes per pixel mode)
	
	// automatic member variables
	int						  fpsMode;						// fps mode (FIXED30FPS, FIXED60FPS or VARIABLEFPS)
	D3DCOLOR				  bgColor;						// window background color
	LPDIRECT3D9				  d3d;							// direct3D object
	D3DPRESENT_PARAMETERS	  d3dPP;						// configuration of d3d backbuffer
	D3DLOCKED_RECT			  lockedTex;					// locked drawing texture area 
	LARGE_INTEGER			  lostDeviceTime;				// the time the device was lost	

	// private member functions
	void                      RenderLock();					// lock the surface for the sprite renderer
	void                      RenderUnLock();				// unlock the surface after sprite rendering

protected:
	// members can be acessed in derived classes
	Font *					  sysFont;						// default engine system font

	void                      LockScreen();					// lock the screen for exclusive access to the video ram
	void                      UnLockScreen();				// unlock the screen for free video ram access to others
	LARGE_INTEGER             GetLostDevTime(); 			// get the time the device was lost		

	// these methods can be overriden in the derived classes
	// making them virtual, makes the compiler use dynamic binding
	virtual void              UnloadResources();			// unload resources when device is lost
	virtual void              LoadResources();				// reload resources after device is operational
	
public:
	D3DGame();												// constructor
	~D3DGame();												// destructor

	// static D3DGame methods
	static LPDIRECT3DDEVICE9  GetDevice();					// get the direct3D device
	static LPDIRECT3DSURFACE9 GetBackBuffer();				// get the back buffer
	static LPD3DXSPRITE       GetRenderer();				// get the sprite renderer
	
	static void PlotPixel(int x, int y, DWORD color);		// plot pixels without clipping

	// D3DGame methods
	void SetFPS(int mode);									// set fixed or variable FPS, default is fixed to 30FPS
					
	// overriden methods from WindowsGame
	void SetWindowBgColor(int r, int g, int b);				// set window bakground color using Direct3D
	bool Initialize(HINSTANCE appInstance);					// initialize window and direct3d surfaces
	bool Reset();											// reset a lost device
	int  Start();											// start the game loop
};

/**********************************************************************************/

// InLine Member Functions

// lock the backbuffer for sprite rendering
inline void D3DGame::RenderLock()
{ renderer->Begin(D3DXSPRITE_ALPHABLEND); }

// unlock the backbuffer after sprite rendering
inline void D3DGame::RenderUnLock()
{ renderer->End(); }

// plot pixels without clipping
inline void D3DGame::PlotPixel(int x, int y, DWORD color)	
 { videoMemory[x + y * videoMemoryPitch] = color; }

// get the time the device was lost
inline LARGE_INTEGER D3DGame::GetLostDevTime()
{ return lostDeviceTime; }

// get the direct3D device
inline LPDIRECT3DDEVICE9 D3DGame::GetDevice()
{ return d3dDev; }

// get the back buffer
inline LPDIRECT3DSURFACE9 D3DGame::GetBackBuffer()
{ return backBuffer; }

// get the sprite renderer
inline LPD3DXSPRITE D3DGame::GetRenderer()
{ return renderer; }

// set fixed or variable FPS, default is fixed to 30FPS
inline void D3DGame::SetFPS(int mode)
{ fpsMode = mode; }

// set window bakground color using Direct3D
inline void D3DGame::SetWindowBgColor(int r, int g, int b)
{ bgColor = D3DCOLOR_XRGB(r,g,b); }

/**********************************************************************************/

#endif